/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "cast_engine_dlna_service_stub.h"
#include "cast_engine_dlna_service.h"
#include "ui/rs_surface_node.h"
#include "window.h"
#include "dlna_stream_manager.h"
#include "dlna_controller.h"
#include "cast_engine_log.h"
#include <cstdint>


namespace OHOS {
namespace CastEngine {
namespace CastEngineService {
DEFINE_CAST_ENGINE_LABEL("Cast-Engine-Dlna-Service-Stub");

int CastEngineDlnaServiceStub::OnRemoteRequest(uint32_t code,
    MessageParcel &data, MessageParcel &reply, MessageOption &option)
{

    std::u16string localDescriptor = GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    switch (code) {
        case PLAY:{
            int32_t result = Play();
            return result;
        }
        case PAUSE:{
            int32_t result = Pause();
            return result;
        }
        case STOP:{
            int32_t result = Stop();
            return result;
        }
        case FORWORD:{
                int32_t time = data.ReadInt32();
                int32_t result = Forward(time);
                return result;
        }
        case BACKWARD:{
                int32_t time = data.ReadInt32();
                int32_t result = Backward(time);
                return result;
        }
        case GET_PLAYER_STATUS:{
                int position = 0;
                int32_t result = GetPosition(position);
                if (result == 0) {
                    reply.WriteInt32(position);
                }
                return result;
        }
        case GET_POSITION:{
                int32_t status;
                int32_t result = GetPlayerStatus(status);
                if (result == 0) {
                    reply.WriteInt32(status);
                }
                return result;
        }
        case GET_DURATION:{
                int32_t Duration;
                int32_t result = GetDuration(Duration);
                if (result == 0) {
                    reply.WriteInt32(Duration);
                }
                return result;
        }
        case SEEK:{
                int32_t position = 0;
                data.ReadInt32(position);
                int32_t result = Seek(position);
                return result;
        }
        case SET_SURFACE:{
            int32_t result = -1;
            sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
            if (!remoteObj) {
                return result;
            }
            sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(remoteObj);
            if (!producer) {
                return result;
            }
            result = SetDmrSurface(producer);
            return result;
        }
        case SEND_DMR_PLAYER: {
            int32_t result = -1;
            sptr<IRemoteObject> object = data.ReadRemoteObject();
            if(!object) {
                reply.WriteInt32(ERR_NULL_OBJECT);
                return ERR_NULL_OBJECT;
            }
            result = SendDmrPlayer(object);
            reply.WriteInt32(result);
            return ERR_NONE;
        }
        case DLNA_GET_DEVICE_NAME: {
            std::string deviceName;
            int32_t result = GetDlnaDeviceName(deviceName);
            CLOGD("CastEngineDlnaServiceStub::GetDlnaDeviceName %{public}s",deviceName.c_str());
            if (result == 0) {
                reply.WriteString(deviceName);
            }
            return result;
        }
        case DLNA_SET_DEVICE_NAME: {
            int32_t result = -1;
            std::string deviceName = data.ReadString();
            if (deviceName.empty()) {
                return result;
            }
            result = SetDlnaDeviceName(deviceName);
            return result;
        }
        case DLNA_GET_DMR_STATES: {
            DmrStates dmrState;
            int result = GetDmrState(dmrState);
            reply.WriteInt32(static_cast<int32_t>(dmrState));
            return result;
        }
        case DLNA_GET_VOLUME: {
            int32_t volume = 0;
            int32_t maxVolume = 0;
            int32_t result = GetVolume(volume,maxVolume);
            reply.WriteInt32(volume);
            reply.WriteInt32(maxVolume);
            return result;
        }
        case DLNA_SET_VOLUME: {
            int32_t volume = 0;
            data.ReadInt32(volume);
            int32_t result = SetVolume(volume);
            return result;
        }
        case AUTH_DEVICE_FAIL: {
            int32_t result = AuthDeviceFail();
            return result;
        }
        default:
            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
    }
}

} // namespace CastEngineService
} // namespace CastEngine
} // namespace OHOS